home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Stuff / 3D_Reality / 3D_Reality_API / Examples / PatchMesh.bproj / PatchMesh.m < prev    next >
Encoding:
Text File  |  1992-10-10  |  4.4 KB  |  192 lines

  1.  
  2. #import "../Stone3DAPI/Stone3D.h"
  3.  
  4. #import "PatchMesh.h"
  5. #import "../ControlPoint.bproj/ControlPoint.h"
  6. #import "GetUVValue.h"
  7. #import <ri/ri.h>
  8.  
  9. #define BEZIER        0
  10. #define HERMITE     1
  11. #define BSPLINE        2
  12. #define CATMULLROM    3
  13.  
  14.  
  15. @implementation PatchMesh
  16.  
  17. - init
  18. {
  19.     id        temp, lastShape;
  20.     int        u,v;
  21.     float    incU, incV;
  22.     float    tempV;
  23.  
  24.     nu = 4; nv = 4;
  25.     if (![[GetUVValue instance] runModalForControl:&nu :&nv])
  26.     return nil;
  27.     
  28.     [super init];
  29.     
  30.     NX_ZONEMALLOC([self zone], thePoints, RtPoint, (nu*nv));
  31.         
  32.     // Create and link control point objects
  33.     controlPts = [[List allocFromZone:[self zone]] initCount:16];
  34.     incU = 2.0 / (nu - 1);
  35.     incV = 2.0 / (nv - 1);
  36.     temp = nil;
  37.     for (v=0; v<nv; v++) {
  38.     tempV = -1.0 + v*incV;
  39.     for (u=0; u<nu; u++) {
  40.         lastShape = temp;
  41.         temp = [[ControlPoint allocFromZone:[self zone]] init];
  42.         [temp translateTo:(-1.0 + u*incU) :tempV :0.0];
  43.         if (v || u) {
  44.         [lastShape linkPeer:temp];
  45.         [controlPts addObject:temp];
  46.         } else {        
  47.         [temp linkAncestor:self];
  48.         [self linkDescendant:temp];
  49.         [controlPts addObject:temp];
  50.         }
  51.     }
  52.     }
  53.  
  54.     uBasis = BEZIER; bcopy(&RiBezierBasis,&theUBasis,sizeof(RtBasis));
  55.     vBasis = BEZIER; bcopy(&RiBezierBasis,&theVBasis,sizeof(RtBasis));
  56.     uStep = 3;
  57.     vStep = 3;
  58.     showCtlPoints = YES;
  59.     showHull = NO;
  60.     
  61.     // The X min and max
  62.     boundingBox[0] = 999;  boundingBox[1] = -999;
  63.     
  64.     // The Y min and max
  65.     boundingBox[2] = 999;  boundingBox[3] = -999;
  66.     
  67.     // The Z min and max
  68.     boundingBox[4] = 999;  boundingBox[5] = -999;
  69.     
  70.     return self;
  71. }
  72.  
  73. - (BOOL) hullVisible
  74. {
  75.     return showHull;
  76. }
  77.  
  78. - setHullVisible:(BOOL) flag
  79. {
  80.     showHull = flag;
  81.     return self;
  82. }
  83.  
  84. - (BOOL)controlVisible
  85. {
  86.     return showCtlPoints;
  87. }
  88.  
  89. - setControlVisible:(BOOL)flag
  90. {
  91.     int        i, max;
  92.     id       *clist;
  93.     
  94.     showCtlPoints = flag;
  95.     for (i=0, max = [controlPts count],
  96.     clist=NX_ADDRESS(controlPts);i<max;i++) {
  97.         [clist[i] setVisible:flag];
  98.     }
  99.     return self;
  100. }
  101.  
  102. - (int)uBasis
  103. {
  104.     return uBasis;
  105. }
  106.  
  107. - setUBasis:(int)theU
  108. {
  109.     uBasis = theU;
  110.     switch (uBasis) {
  111.     case BEZIER: bcopy(&RiBezierBasis,&theUBasis,sizeof(RtBasis));
  112.              uStep = RI_BEZIERSTEP;
  113.              break;
  114.     case HERMITE: bcopy(&RiHermiteBasis,&theUBasis,sizeof(RtBasis));
  115.              uStep = RI_HERMITESTEP;
  116.              break;
  117.     case BSPLINE: bcopy(&RiBSplineBasis,&theUBasis,sizeof(RtBasis));
  118.              uStep = RI_BSPLINESTEP;
  119.              break;
  120.     case CATMULLROM: bcopy(&RiCatmullRomBasis,&theUBasis,sizeof(RtBasis));
  121.              uStep = RI_CATMULLROMSTEP;
  122.              break;
  123.     default: bcopy(&RiBezierBasis,&theUBasis,sizeof(RtBasis));
  124.     }
  125.     return self;
  126. }
  127.  
  128. - (int)vBasis 
  129. {
  130.     return vBasis;
  131. }
  132.  
  133. - setVBasis:(int)theV
  134. {
  135.     vBasis = theV;
  136.     switch (vBasis) {
  137.     case BEZIER: bcopy(&RiBezierBasis,&theVBasis,sizeof(RtBasis));
  138.              vStep = RI_BEZIERSTEP;
  139.              break;
  140.     case HERMITE: bcopy(&RiHermiteBasis,&theVBasis,sizeof(RtBasis));
  141.              vStep = RI_HERMITESTEP;
  142.              break;
  143.     case BSPLINE: bcopy(&RiBSplineBasis,&theVBasis,sizeof(RtBasis));
  144.              vStep = RI_BSPLINESTEP;
  145.              break;
  146.     case CATMULLROM: bcopy(&RiCatmullRomBasis,&theVBasis,sizeof(RtBasis));
  147.              vStep = RI_CATMULLROMSTEP;
  148.              break;
  149.     default: bcopy(&RiBezierBasis,&theVBasis,sizeof(RtBasis));
  150.     }
  151.     return self;
  152. }
  153.  
  154. // In order to provide selection capablity we must override Shapes instance 
  155. // method calcBoundingBox.
  156. - calcBoundingBox
  157. {
  158.     int        i, max;        // index variables
  159.     id       *clist;        // Address of control point list
  160.     RtFloat     pt[3];
  161.     
  162.     for (i=0, max = [controlPts count],
  163.     clist=NX_ADDRESS(controlPts);i<max;i++) {
  164.         [clist[i] trans:&pt[0]];
  165.         if (pt[0] < boundingBox[0]) boundingBox[0] = pt[0];
  166.         if (pt[0] > boundingBox[1]) boundingBox[1] = pt[0];
  167.         if (pt[1] < boundingBox[2]) boundingBox[2] = pt[1];
  168.         if (pt[1] > boundingBox[3]) boundingBox[3] = pt[1];
  169.         if (pt[2] < boundingBox[4]) boundingBox[4] = pt[2];
  170.         if (pt[2] > boundingBox[5]) boundingBox[5] = pt[2];
  171.     }
  172.     return self;
  173. }
  174.  
  175. - doRenderSelf: (Camera *) camera
  176. {
  177.     int        i, max;
  178.     id       *clist;
  179.     
  180.     [super doRenderSelf:camera]; 
  181.     for (i=0, max = [controlPts count],
  182.     clist=NX_ADDRESS(controlPts);i<max;i++) {
  183.         [clist[i] trans:&thePoints[i][0]];
  184.     }
  185.     RiBasis(theUBasis, (RtInt)uStep, theVBasis, (RtInt)vStep);
  186.     RiPatchMesh(RI_BICUBIC,(RtInt)nu,RI_NONPERIODIC,(RtInt)nv,RI_NONPERIODIC,
  187.         RI_P,(RtPointer)thePoints,RI_NULL);
  188.     
  189.     return self;
  190. }
  191. @end
  192.